//
//  SelectPhotoView.m
//  Rail
//
//  Created by hychen on 2018/3/3.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "SelectPhotoView.h"
#import "SelectPhotoCollectionViewLayout.h"
#import "SelectPhotoCollectionViewCell.h"

//#define imageWH 100
//#define padding (SCREEN_WIDTH-imageWH*maxCount))/maxCount

@interface SelectPhotoView ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UILabel *titleLabel;
/***  照片*/
@property (nonatomic, weak) UICollectionView *collectionView;
/***  显示图片数组*/
@property (nonatomic, strong) NSMutableArray *showImagesArray;
@end
@implementation SelectPhotoView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
        _maxSelectImageCount = 2;
    }
    return self;
}

- (void)setupSubviews {
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:18.f];
    titleLabel.textColor = Color_RGB(44, 44, 44);
    self.titleLabel = titleLabel;
    [self addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(15);
    }];
    
    __weak typeof (self) weakSelf = self;
    SelectPhotoCollectionViewLayout *flowLayout = [[SelectPhotoCollectionViewLayout alloc] init];
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    collectionView.translatesAutoresizingMaskIntoConstraints = NO;
    [collectionView registerClass:[SelectPhotoCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([SelectPhotoCollectionViewCell class])];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self addSubview:collectionView];
    self.collectionView = collectionView;
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf);
    }];
//    [imageButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo();
//    }];
}

#pragma mark - 属性
- (void)setSelectedImageArray:(NSMutableArray *)selectedImageArray
{
    _selectedImageArray = selectedImageArray;
    NSMutableArray *imageArray;
    if (selectedImageArray.count) {
        imageArray = [NSMutableArray arrayWithArray:selectedImageArray];
    } else {
        imageArray = [NSMutableArray array];
    }
    if (selectedImageArray.count < _maxSelectImageCount) {
        SelectPhotoModel *model = [SelectPhotoModel selectImageModel];
        [imageArray addObject:model];
    }
    self.showImagesArray = imageArray;
    [self.collectionView reloadData];
}

#pragma mark - collectionView代理
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.showImagesArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof (self) weakSelf = self;
    SelectPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([SelectPhotoCollectionViewCell class]) forIndexPath:indexPath];
    cell.model = [self.showImagesArray objectAtIndex:indexPath.row];
    
    cell.deletePhotoBlock = ^(SelectPhotoModel *model){
        // 删除操作
        [weakSelf deletePhotoWithModel:model];
    };
    
    cell.seletedPhotoBlock = ^(SelectPhotoModel *model){
        // 选择图片操作
        [weakSelf selectPhotoWithModel:model];
    };
    
    // 调整选择照片尺寸
    if (self.showImagesArray.count > 1 && indexPath.row == self.showImagesArray.count - 1 && self.selectedImageArray.count < 9) {
        //        [cell resizeButtonWithSmall:YES];
    } else {
        [cell resizeButtonWithSmall:NO];
    }
    
    return cell;
}

// 删除照片
- (void)deletePhotoWithModel:(SelectPhotoModel *)model
{
    if (self.deletePhotoBlock) {
        self.deletePhotoBlock(model);
    }
}

// 选择图片操作
- (void)selectPhotoWithModel:(SelectPhotoModel *)model
{
    if (model.isSelectPhoto) {
        if (self.selectPhotoBlock) {
            self.selectPhotoBlock();
        }
    } else {
        if (self.editPhotoBlock && model.uploadImage) {
            self.editPhotoBlock(model);
        }
    }
}

// 刷新数据
- (void)reloadData
{
    [self.collectionView reloadData];
}

@end
